﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using SO_DataFetcher.PubFunctions;
using SO_DataFetcher.PubFunctions.Actions;
using SO_DataFetcher.PubFunctions.DataLink;

namespace SO_DataFetcher.Forms
{
    public partial class FrmAction : Form
    {

        private readonly string _actionIndex; 
        public ActionStruct TempAction;
        private ActionStruct _oldAction;
        private readonly string _operateType;

        public FrmAction(string actionIndex, string type)
        {
            InitializeComponent();

            this._operateType = type;

            switch (_operateType)
            {
                case "Add":
                    this._actionIndex = string.Empty;
                    this.TempAction = new ActionStruct();
                    break;
                case "Edit":
                    this._actionIndex = actionIndex;

                    this.TempAction = PubAction.Actions[this._actionIndex];

                    if (!PubAction.Actions.ContainsKey(this.TempAction.Index))
                    {
                        MessageBox.Show(@"没有查询到对应的数据库连接，请重新连接。");
                        this.Close();
                    }

                    this._oldAction = PubAction.Actions[this._actionIndex];

                    break;
            }
        }


        /// <summary>
        /// 初始化窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAction_Load(object sender, EventArgs e)
        {
            switch (this._operateType)
            {
                case "Add":
                    this.txtName.Text = string.Empty;
                    this.txtDataLink.Text = string.Empty;
                    this.txtOutPath.Text = PubCommon.MainPath + @"\OutPath";
                    this.txtSqls.Text = string.Empty;
                    break;
                case "Edit":
                   this.txtName.Text = this.TempAction.Name;
                    this.txtDataLink.Text = this.TempAction.DataLink;
                    this.txtOutPath.Text = this.TempAction.OutPath;
                    this.txtSqls.Text = this.TempAction.Sql.Replace(",", "\r\n");
                    break;
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (_operateType.Equals("Edit"))
            {
                if (CheckChanged())
                {
                    CheckFolderExist();
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(@"当前数据没有发生变化");
                    this.DialogResult = DialogResult.Cancel;
                }
            }
            else if (_operateType.Equals("Add"))
            {
                this.TempAction.Index = SetActionIndex();

                CheckFolderExist();
                this.DialogResult = DialogResult.OK;
            }
        }


        /// <summary>
        /// 判断输出路径是否存在
        /// </summary>
        private void CheckFolderExist()
        {
            if (Directory.Exists(TempAction.OutPath))
                Directory.CreateDirectory(TempAction.OutPath);
        }

        /// <summary>
        /// 设定Index
        /// </summary>
        /// <returns></returns>
        private string SetActionIndex() 
        {
            string index = string.Empty;

            DateTime dateTime = DateTime.Now;

            index = dateTime.Year.ToString() + dateTime.Month.ToString() + dateTime.Day.ToString() +
                    dateTime.Hour.ToString() + dateTime.Minute.ToString() + dateTime.Second.ToString();

            return index;
        }

        /// <summary>
        /// 判断是否已经发生变化
        /// </summary>
        /// <returns></returns>
        private bool CheckChanged()
        {
            if (!TempAction.Name.Equals(_oldAction.Name))
                return true;

            if (!TempAction.DataLink.Equals(_oldAction.DataLink))
                return true;

            if (!TempAction.OutPath.Equals(_oldAction.OutPath))
                return true;

            if (!TempAction.Sql.Equals(_oldAction.Sql))
                return true;

            return false;
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            this.TempAction.Name = txtName.Text.Trim();
        }

        private void txtDataLink_TextChanged(object sender, EventArgs e)
        {
            this.TempAction.DataLink = txtDataLink.Text.Trim();
        }

        private void txtOutPath_TextChanged(object sender, EventArgs e)
        {
            this.TempAction.OutPath = txtOutPath.Text.Trim();
        }

        private void txtSqls_TextChanged(object sender, EventArgs e)
        {
            this.TempAction.Sql = txtSqls.Text.Trim().Replace("\r\n", ",");
        }

        /// <summary>
        /// 选择路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEditOutPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog  = new FolderBrowserDialog();
            folderBrowserDialog.ShowNewFolderButton = true;
            if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.txtOutPath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnEditDatalink_Click(object sender, EventArgs e)
        {
            
            FrmList frmList = new FrmList("DataLink",new string[]{this.txtDataLink.Text.Trim()});
            if (frmList.ShowDialog(this) == DialogResult.OK)
            {
                this.txtDataLink.Text = frmList._keyStrings[0];
            }
        }

        private void btnEditSqls_Click(object sender, EventArgs e)
        {
            FrmList frmList = new FrmList("Scripts", new string[] { this.txtDataLink.Text.Trim() });
            if (frmList.ShowDialog(this) == DialogResult.OK)
            {
                string scripts = string.Join("\r\n", frmList._keyStrings);
                this.txtDataLink.Text = scripts;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

    }
}
